package com.rocket.activity;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

import com.rocket.R;
import com.rocket.database.DataBaseOpenHelper;

public class MainActivity extends Activity {

	private ProgressBar			mProgressBar;
	private Button				mButton;

	private InitDataBaseHandler	mInitDataBaseHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mProgressBar = (ProgressBar) findViewById(R.id.progressbar);

		mButton = (Button) findViewById(R.id.button);
		mButton.setVisibility(View.INVISIBLE);
		mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ChoseFirearmActivity.class);
				startActivity(intent);
			}
		});

		mInitDataBaseHandler = new InitDataBaseHandler();
		new InitDataBaseThread().start();
			
		File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Rocket/");
		if(!file.exists()){
			file.mkdirs();
		}
	}

	private class InitDataBaseThread extends Thread {

		@Override
		public void run() {
			DataBaseOpenHelper.initDataBaseContext(MainActivity.this);
			mInitDataBaseHandler.obtainMessage().sendToTarget();
		}
	}

	private class InitDataBaseHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			mProgressBar.setVisibility(View.INVISIBLE);
			mButton.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onStart() {
		int flag = 0;
		flag = getIntent().getIntExtra("Flag", 0);
		if (flag == 1) {
			// 退出程序
			this.finish();
		}
		super.onStart();
	}
}
